<template>
  <div class="search-nav">
    <el-row>
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-col :span="24">
          <el-form-item label="所属管廊：">
            <el-select
              v-model="formInline.pipeId"
              class="m-2"
              placeholder="--请选择--"
            >
              <el-option
                v-for="item in pipeArray"
                :key="item.id"
                :label="item.pipeName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属仓室：">
            <el-select
              v-model="formInline.storehouseId"
              class="m-2"
              placeholder="--请选择--"
            >
              <el-option
                v-for="item in warehouseArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="防火分区：">
            <el-select
              v-model="formInline.subregionId"
              class="m-2"
              placeholder="--请选择--"
            >
              <el-option
                v-for="item in fireproofArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设备名称：">
            <el-input
              class="innerInput"
              v-model="formInline.equipmentName"
              placeholder="--请输入--"
              size="large"
              input-style
            />
          </el-form-item>
          <el-form-item label="告警时间：">
            <el-date-picker
              v-model="time"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:mm:ss"
              start-placeholder="开始时间"
              range-separator="~"
              end-placeholder="结束时间"
              @change="change"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-form-item class="btns">
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="success" @click="onClear">清空</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script setup>
import { reactive, defineEmits, ref } from 'vue'
import { pipelist, warehouselist, fireprooflist } from '@/api/back'

const formInline = reactive({
  pipeId: '',
  storehouseId: '',
  subregionId: '',
  startTime: '',
  endTime: '',
  equipmentName: ''
})
const time = ref()
const pipeArray = ref([])
const warehouseArray = ref([])
const fireproofArray = ref([])
const getListData = async () => {
  pipeArray.value = await pipelist()
  warehouseArray.value = await warehouselist()
  fireproofArray.value = await fireprooflist()
}
getListData()

const emits = defineEmits(['onSearch'])
const onSubmit = () => {
  console.log(formInline)
  emits('onSearch', formInline)
}

const change = (res) => {
  if (res) {
    formInline.startTime = res[0]
    formInline.endTime = res[1]
  } else {
    formInline.startTime = ''
    formInline.endTime = ''
  }
}

const onClear = () => {
  formInline.pipeId = ''
  formInline.storehouseId = ''
  formInline.subregionId = ''
  formInline.endTime = ''
  formInline.startTime = ''
  formInline.equipmentName = ''
  time.value = ''
  console.log(formInline)
  emits('onSearch', formInline)
}
</script>

<style lang="scss" scoped>
.form-inline {
  width: 100%;
  position: relative;
  column-gap: 20px;
  // padding-bottom: 15px;
  :deep(.el-form-item) {
    margin-right: 10px !important;
  }
  .btns {
    position: absolute;
    right: 0;
    top: 4px;
    margin-right: 0px !important;
  }
}
:deep(.el-input__inner) {
  width: 247px !important;
}
</style>
